﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace SmartSpace.Core
{
    public class CoreContext : DomainContext
    {
        public CoreContext(DbContext dbContext)
            : base(dbContext)
        {

        }

        public static void Build(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ImageFile>().ToTable("__ImageFile");
            modelBuilder.Entity<ImageFile>().HasKey(p => p.Id);

            modelBuilder.Entity<User>().ToTable("__User");
            modelBuilder.Entity<User>().HasKey(p => p.Id);
            modelBuilder.Entity<User>().Property(p => p.Id).IsRequired();
            modelBuilder.Entity<User>().HasMany(u => u.UserGroups).WithMany()
                .Map(p => { p.MapLeftKey("UserId"); p.MapRightKey("UserGroupId"); p.ToTable("__RelationUserToUserGroup"); });
            //modelBuilder.Entity<User>().HasOptional<Role>(p => p.Role).WithMany()
            //    .HasForeignKey(p => p.RoleId);

            modelBuilder.Entity<IntegrationUser>().ToTable("__IntegrationUser");
            modelBuilder.Entity<IntegrationUser>().HasKey(p => p.Id);
            modelBuilder.Entity<IntegrationUser>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<Role>().ToTable("__Role");
            modelBuilder.Entity<Role>().HasKey(p => p.Id);
            modelBuilder.Entity<Role>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<UserRole>().ToTable("core_userrole");
            modelBuilder.Entity<UserRole>().HasKey(p => p.Id);
            modelBuilder.Entity<UserRole>()
                .HasRequired<Role>(p => p.Role).WithMany().HasForeignKey(p => p.RoleId)
                .WillCascadeOnDelete(false);
            //modelBuilder.Entity<UserRole>()
            //    .HasRequired<User>(p => p.User).WithMany().HasForeignKey(p => p.UserId)
            //    .WillCascadeOnDelete(false);

            modelBuilder.Entity<Application>().ToTable("__Application");
            modelBuilder.Entity<Application>().HasKey(p => p.Id);
            modelBuilder.Entity<Application>().Property(p => p.Id).IsRequired();

            modelBuilder.Entity<Theme>().ToTable("__Theme");
            modelBuilder.Entity<Theme>().HasKey(p => p.Id);

            modelBuilder.Entity<ApplicationModule>().ToTable("__ApplicationModule");

            modelBuilder.Entity<Module>().ToTable("__Module");
            modelBuilder.Entity<Module>().HasKey(p => p.Id);
            modelBuilder.Entity<Module>().HasMany(p => p.Views)
                .WithRequired(p=>p.Module).HasForeignKey(p => p.ModuleId);

            modelBuilder.Entity<ModuleView>().ToTable("__ModuleView");
            modelBuilder.Entity<ModuleView>().HasKey(p => p.Id);

            modelBuilder.Entity<Navigation>().ToTable("__Navigation");
            modelBuilder.Entity<Navigation>().HasKey(p => p.Id);
            modelBuilder.Entity<Navigation>()
                .HasMany(p => p.Children)
                .WithOptional()
                .HasForeignKey(p => p.ParentId);

            modelBuilder.Entity<Shortcut>().ToTable("__Shortcut");
            modelBuilder.Entity<Shortcut>().HasKey(p => p.Id);

            modelBuilder.Entity<RoleNavigation>().ToTable("__RoleNavigation");
            modelBuilder.Entity<RoleNavigation>().HasKey(p => p.Id);

            modelBuilder.Entity<Privilege>().ToTable("core_privilege");
            modelBuilder.Entity<Privilege>().HasKey(p => p.Id);

            modelBuilder.Entity<UserGroup>().ToTable("__UserGroup");
            modelBuilder.Entity<UserGroup>().HasKey(p => p.Id);

            modelBuilder.Entity<VirtualPage>().ToTable("core_virtualpage");
            modelBuilder.Entity<VirtualPage>().HasKey(p => p.Id);
            modelBuilder.Entity<VirtualPage>().HasRequired(p => p.Template)
                .WithMany().HasForeignKey(p => p.TemplateId);

            //modelBuilder.Entity<VirtualMenu>().ToTable("core_virtualmenu");
            //modelBuilder.Entity<VirtualMenu>().HasKey(p => p.Id);
            //modelBuilder.Entity<VirtualMenu>().HasMany(p => p.Children)
            //    .WithOptional().HasForeignKey(p => p.ParentId);

            modelBuilder.Entity<PageTemplate>().ToTable("core_pagetemplate");
            modelBuilder.Entity<PageTemplate>().HasKey(p => p.Id);

            modelBuilder.Entity<DynamicPagePersonalization>().ToTable("__DynamicPagePersonalization");
            modelBuilder.Entity<DynamicPagePersonalization>().HasKey(p => p.Id);
        }
    }
}
